package com.voc.im.controller.account;

import com.jfoenix.controls.JFXSnackbar;
import com.jfoenix.controls.JFXSnackbarLayout;
import com.voc.im.model.AccountModel;
import com.voc.im.xmpp.IMConnection;
import io.datafx.controller.FXMLController;
import io.datafx.controller.flow.Flow;
import io.datafx.controller.flow.FlowException;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.util.Duration;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

@FXMLController("/fxml/account2.fxml")
public class Account2Controller {

    @FXML
    private StackPane stackPaneBackground;
    @FXML
    private StackPane stackPaneAccount;

    @Inject
    private AccountModel accountModel;

    @PostConstruct
    public void init() throws Exception {

        stackPaneBackground.setStyle("-fx-background-image: url(https://cn.bing.com/th?id=OHR.RubySunset_ZH-CN5544596519_1920x1080.jpg);");

        accountModel.getActionProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == null) {
                return;
            }
            if (newValue.equals("login")) {
                craterLoginPane();
            } else if (newValue.equals("register")) {
                craterRegisterPane();
            } else if (newValue.equals("vcard")) {
                craterVCardPane();
            } else {
                craterStatusPane();
            }
        });
        //默认加载创建登录页
        accountModel.setActionProperty("login");
    }

    /**
     * 创建登录页
     */
    private void craterLoginPane() {
        try {
            Flow flow = new Flow(AccountLoginController.class);
            StackPane stackPane = flow.start();
            stackPaneAccount.getChildren().setAll(stackPane);
        } catch (FlowException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建注册页
     */
    private void craterRegisterPane() {
        try {
            Flow flow = new Flow(AccountRegisterController.class);
            StackPane stackPane = flow.start();
            stackPaneAccount.getChildren().setAll(stackPane);
        } catch (FlowException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建完善信息页面
     */
    private void craterVCardPane() {
        try {
            Flow flow = new Flow(AccountVCardController.class);
            StackPane stackPane = flow.start();
            stackPaneAccount.getChildren().setAll(stackPane);
        } catch (FlowException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建状态页
     */
    private void craterStatusPane() {
        try {
            Flow flow = new Flow(AccountStatusController.class);
            StackPane stackPane = flow.start();
            stackPaneAccount.getChildren().setAll(stackPane);
        } catch (FlowException e) {
            e.printStackTrace();
        }
    }

}
